import random

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from Two.models import Student


def index(request):
    return HttpResponse('Two index')


def addStudent(request):
    ##创建对象实例
    student = Student()
    ##定义字段数据
    student.s_name = 'Jerry%d' % random.randrange(0,100)
    ##调用插入函数 .save()
    student.save()
    return HttpResponse("Add Success " + student.s_name)


def getStudent(request):

    ##获取Student模型对象
    students = Student.objects.all()
    for student in students :
        print(student.s_name)

    # return HttpResponse("Student List")
    context = {
        "hobby" : "Play Games",
        "eat" : "meat",
        "students" : students
    }

    return render(request,'student_list.html',context=context)


def updateStudent(request):
    # 取需要修改的数据(primary key  = 2)
    student = Student.objects.get(pk = 2)
    # 改名字
    student.s_name = "Jack"
    #更改保存
    student.save()

    return HttpResponse("Update Success")


def deleteStudent(request):

    student = Student.objects.get(pk = 3)
    student.delete()

    return HttpResponse("Delete Success")